package com.codeoss.framework.ureport2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bstek.ureport.export.ExportConfigure;
import com.bstek.ureport.export.ExportConfigureImpl;
import com.bstek.ureport.export.ExportManager;
import com.bstek.ureport.export.html.HtmlReport;
import com.codeoss.module.ir.model.IrReport;
import com.codeoss.module.ir.service.IIrReportService;

@Component
public class UReport2Util implements ApplicationContextAware{
	private ApplicationContext applicationContext;
	@Value("${framework.base-path}")
	private String basePath;
	@Autowired
	private IIrReportService irReportService;
	public HtmlReport exportHtmlReport(String reportCode,Map<String, Object> paraMap) {
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", reportCode);
		IrReport irReport=irReportService.getOne(query);
		
		ExportManager exportManager=(ExportManager)applicationContext.getBean(ExportManager.BEAN_ID);
		if(paraMap==null) {
			paraMap=new HashMap<String,Object>();
		}
		String filename=UUID.randomUUID().toString();
		File file=new File("d:/ureportfiles",filename);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			fos.write(irReport.getJsonContent().getBytes());
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
		HtmlReport htmlReport=exportManager.exportHtml("file:"+filename, "", paraMap);
		return htmlReport;
	}
	
	public void dataExport(String reportCode,Map<String, Object> paraMap,String format, OutputStream os) {
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", reportCode);
		IrReport irReport=irReportService.getOne(query);
		
		ExportManager exportManager=(ExportManager)applicationContext.getBean(ExportManager.BEAN_ID);
		if(paraMap==null) {
			paraMap=new HashMap<String,Object>();
		}
		String filename=UUID.randomUUID().toString();
		File file=new File("d:/ureportfiles",filename);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			fos.write(irReport.getJsonContent().getBytes());
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		try {
			ExportConfigure exportConfigure=new ExportConfigureImpl("file:"+filename,paraMap,os);
			if("pdf".equals(format)) {
				exportManager.exportPdf(exportConfigure);
			}else if("excel".equals(format)) {
				exportManager.exportExcel(exportConfigure);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}finally {
			
		}
	}
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}
}
